FROM alpine:3.12.0 as builder
LABEL maintainer Louis <379323664@qq.com>

#定义环境变量
ENV TENGINE_VERSION=2.3.3 LUA_JIT2=2.1-20220411 HEAD_MORE=0.34 COOKIE=1.1.0 NDK=0.3.1 PREFIX=/service/nginx
#定义编译参数
ENV CONFIG "\
    --user=webrunner \
    --group=webrunner \
    --prefix=$PREFIX/ \
    --sbin-path=/usr/sbin/nginx \
    --modules-path=/usr/lib64/nginx/modules \
    --conf-path=$PREFIX/nginx.conf \
    --http-log-path=/var/log/nginx/access.log \
    --error-log-path=/var/log/nginx/error.log \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/run/nginx.lock \
    --with-http_secure_link_module \
    --with-http_image_filter_module \
    --with-http_random_index_module \
    --with-threads \
    --with-http_ssl_module \
    --with-http_sub_module \
    --with-http_stub_status_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_realip_module \
    --with-compat \
    --with-file-aio \
    --with-http_dav_module \
    --with-http_degradation_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_xslt_module \
    --with-http_auth_request_module \
    --with-http_addition_module \
    --with-http_v2_module \
    --with-pcre \
    --with-ld-opt=-Wl,-rpath,/usr/local/LuaJIT/lib \
    --add-module=modules/ngx_http_upstream_check_module \
    --add-module=modules/ngx_http_upstream_session_sticky_module \
    --add-module=modules/ngx_http_upstream_dynamic_module \
    --add-module=modules/ngx_http_upstream_consistent_hash_module \
    --add-module=modules/ngx_http_upstream_dyups_module \
    --add-module=modules/ngx_http_user_agent_module \
    --add-module=modules/ngx_http_proxy_connect_module \
    --add-module=modules/ngx_http_concat_module \
    --add-module=modules/ngx_http_footer_filter_module \
    --add-module=modules/ngx_http_sysguard_module \
    --add-module=modules/ngx_http_slice_module \
    --add-module=modules/nginx_cookie_flag_module-$COOKIE \
    --add-dynamic-module=modules/ngx_http_lua_module \
    --add-dynamic-module=modules/ngx_devel_kit-0.3.1 \
    --add-module=modules/headers-more-nginx-module-$HEAD_MORE \
    --with-http_geoip_module=dynamic \
    --http-client-body-temp-path=/var/cache/nginx/client_temp \
    --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
    --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
    --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
    --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
    "

#替换apk源
COPY repositories /etc/apk/repositories

#编译安装
RUN \
    mkdir -p /usr/src \
    && cd /usr/src \
    && apk add --no-cache --virtual .build-deps \
     make \
     curl \
     gcc \
     g++ \
     gd-dev \
     geoip-dev \
     libxslt-dev \
     linux-headers \
     zlib-dev \
     openssl-dev \
     pcre-dev \
     readline-dev \
    && curl "https://tengine.taobao.org/download/tengine-$TENGINE_VERSION.tar.gz" -o tengine.tar.gz \
    && tar zxf tengine.tar.gz \
    && cd tengine-$TENGINE_VERSION \
    && curl -L "https://github.91chi.fun//https://github.com/AirisX/nginx_cookie_flag_module/archive/v$COOKIE.tar.gz" -o cookie.tar.gz \
    && curl -L "https://github.91chi.fun//https://github.com/openresty/headers-more-nginx-module/archive/v$HEAD_MORE.tar.gz" -o more.tar.gz \
    && curl -L "https://github.91chi.fun//https://github.com/openresty/luajit2/archive/v$LUA_JIT2.tar.gz" -o $LUA_JIT2.tar.gz \
    && curl -L "https://codeload.github.com/vision5/ngx_devel_kit/tar.gz/refs/tags/v$NDK" -o ndk.tar.gz \
    && tar -zxC /usr/src/tengine-$TENGINE_VERSION/modules -f more.tar.gz \
    && tar -zxC /usr/src/tengine-$TENGINE_VERSION/modules -f cookie.tar.gz \
    && tar -zxC /usr/src/tengine-$TENGINE_VERSION/modules -f ndk.tar.gz \
    && tar -zxC /usr/src/tengine-$TENGINE_VERSION -f $LUA_JIT2.tar.gz \
    && cd luajit2-$LUA_JIT2 && make install PREFIX=/usr/local/LuaJIT \
    && export LUAJIT_LIB=/usr/local/LuaJIT/lib \
    && export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.1 \
    && source /etc/profile \
    && cd .. \
    && ./configure $CONFIG \
    && make -j2 \
    && make install \
    && rm -rf /service/nginx/html /service/nginx/*.default \
    && strip /usr/sbin/nginx \
    && rm -rf /usr/src/tengine-$TENGINE_VERSION



#===== 多阶段构建 ==========================

FROM alpine:3.12.0
ENV TENGINE_VERSION=2.3.3
ENV WWW_ROOT=/service/webindex/
COPY --from=builder /usr/sbin/nginx /usr/sbin/nginx
COPY --from=builder /service/nginx /service/nginx
COPY --from=builder /usr/lib64/nginx /usr/lib64/nginx
COPY --from=builder /usr/local/LuaJIT /usr/local/LuaJIT
COPY repositories /etc/apk/repositories
RUN addgroup -S webrunner \
    && adduser -D -S -h /var/cache/nginx -s /sbin/nologin -G webrunner webrunner \
    && mkdir -p ${WWW_ROOT}/demo/ /var/log/nginx /var/cache/nginx \
    && runDeps="$( \
           scanelf --needed --nobanner /usr/sbin/nginx \
           | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
           | sort -u \
           | xargs -r apk info --installed \
           | sort -u \
           )" \
    && apk add --no-cache --virtual .nginx-rundeps $runDeps gd libxslt geoip pcre-dev openssl-dev readline-dev \
    && apk add --no-cache gettext curl \
    \
    #将日志标准输出
    && ln -sf /dev/stdout /var/log/nginx/access.log \
    && ln -sf /dev/stderr /var/log/nginx/error.log \
    \
    #设置中国时区
    && apk add --no-cache tzdata \
    && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone \
    && rm -rf /usr/src /var/cache/apk/* \
    EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]